import Vue from "vue"
import Vuex from "vuex"
Vue.use(Vuex)
//将数据从本地存储中提取
var car = JSON.parse(localStorage.getItem('car') || '[]')
var store = new Vuex.Store({
    state:{
        car
    },
    mutations:{ //this.$store.commit("modifyCar",'唯一参数')
        // var getinfo = {id:this.id,count:this.selectcount,price:this.card_list.sell_price,secected:true}
        modifyCar(state,getinfo){//借助mutations里面的方法修改car的值
            // 先判断先前是否有件数
            var flag = false;
            //判断之前是否选了该id的商品，如果选择了就直接增加数量就ok
            state.car.some(item => {
                if(item.id == getinfo.id){
                    item.count += getinfo.count;
                    flag = true;
                    return true
                }
            });
            if(!flag){
                state.car.push(getinfo)
            }
            //将数据放到本地存储中
            localStorage.setItem("car",JSON.stringify(state.car))
           
        },
        updateCar(state,getinfo){
            state.car.some(item =>{
                if(item.id ==getinfo.id){
                    item.count = parseInt(getinfo.count)
                    return true
                }
                
            })
             //将数据放到本地存储中
             localStorage.setItem("car",JSON.stringify(state.car))
        },
        removeData(state,id){
            state.car.some((item,i) =>{
                if(item.id== id){
                    state.car.splice(i,1)
                    return true
                }
            })
            //将数据放到本地存储中
            localStorage.setItem("car",JSON.stringify(state.car))
        },
        selectChange(state,info){
            state.car.some(item => {
                if (item.id == info.id){
                    item.selected = info.selected
                    return true
                }
            })
             //将数据放到本地存储中
             localStorage.setItem("car",JSON.stringify(state.car))
        }

        
    },
    getters:{ //this.$store.getters. ***
        getAllCount(state){
            var c = 0;
            state.car.forEach(item => {
                c += parseInt(item.count);
            })
            return c
        },
        getgoodidc(state){
            var l = [];
            state.car.forEach(item => {
                l[item.id] = item.count
            })
            return l
        },
        // 设置一个id:**,selccted:***得对象，这样
        //<mt-switch v-model="value"></mt-switch> 根据里面得value是true/false来确定打开关闭

        getgoodsSelect(state){
            var m = {}
            state.car.forEach(item =>{
                m[item.id] = item.selected
            })
            return m
        },
        //已选商品的总价
        getgoodsCountAndmount(state){
            var li = {
                count:0, //商品的总数量
                amount:0//商品的总价
            }
            state.car.forEach(item =>{
                if(item.selected){
                    li.count += item.count
                    li.amount += item.count * item.price
                }
                
            })
            return li
        }
       
       
    }

})

import MintUI from "mint-ui"
Vue.use(MintUI)
import 'mint-ui/lib/style.css'

import "./index.scss"

//导入vue-resource
import VueResource from "vue-resource"
Vue.use(VueResource)
//vue-resource设置全局变量
Vue.http.options.root = 'http://www.liulongbin.top:3005';
Vue.http.options.emulateJSON = true;

//导入mui
import "./lib/mui/css/mui.min.css"
import "./lib/mui/css/icons-extra.css"

import app from "./App.vue"
//导路由包
import VueRouter from "vue-router"
Vue.use(VueRouter)
//引用路由文件按
import router from "./router.js"

//导入moment 时间处理
import moment from "moment"
Vue.filter("dataFormat",function(data,patten="YYYY-MM-DD HH:mm:ss"){
    return moment(data).format(patten);
})

// 缩略图
import VuePreview from 'vue-preview';
import { stringify } from "querystring";
Vue.use(VuePreview);


var vm = new Vue({
    el:"#app",
    render:c => c(app),
    router,
    store
})